/*
 * Copyright (c) 2025 李伟国
 * SQL-API is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.moyu.sql.handler;

import com.moyu.sql.response.ErrorResponseData;
import com.moyu.sql.response.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(NoResourceFoundException.class)
    public ResponseEntity<ResponseData> handle404(NoResourceFoundException e) {
        log.error("404", e);
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponseData(HttpStatus.NOT_FOUND.value(), e.getMessage()));
    }

    /**
     * 全局异常
     * @param e 异常
     * @return 响应
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseData> handle500(Exception e) {
        log.error("全局异常", e);
        return ResponseEntity.status(HttpStatus.OK).body(new ErrorResponseData(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()));
    }

}
